package com.hspedu.newData.dmsxl.树.二叉树路径.路径;

import com.hspedu.newData.dmsxl.ds.TreeNode;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.PrimitiveIterator;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-04-23 
 * @Version: 1.0
 */

public class LC113路径总和Ⅱ {

    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();

    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        if (root == null) return res;
        getPath(root,targetSum);
        return res;
    }
    public void getPath(TreeNode root,int target){
        path.add(root.val);
        if (root.left == null && root.right == null){
            if (target - root.val == 0){
                res.add(path);
            }
            return;
        }
        if (root.left != null){
            getPath(root.left,target - root.val);
            path.remove(path.size() - 1);
        }
        if (root.right != null){
            getPath(root.right,target - root.val);
            path.remove(path.size() - 1);
        }
    }

    @Test
    public void test() {
        // 测试代码

        TreeNode t7 = new TreeNode(7);
        TreeNode t6 = new TreeNode(4);
        TreeNode t5 = new TreeNode(5);
        TreeNode t4 = new TreeNode(4);
        TreeNode t3 = new TreeNode(3,t6,t7);
        TreeNode t2 = new TreeNode(2,t4,t5);
        TreeNode t1 = new TreeNode(1,t2,t3);

        List b = pathSum(t1, 8);
        System.out.println("==========");

    }

}
